package com.pdf.pdfserver.service.solr;

import com.pdf.pdfserver.config.SolrContext;
import com.pdf.pdfserver.vo.ArticleVO;
import com.pdf.pdfserver.vo.PageVO;
import org.apache.solr.client.solrj.SolrServerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @Author songfz
 * @Description文章数据全文搜索
 * @Date 2022/1/10
 */
@Service
public class ArticleSolrService extends BaseSolrServer<ArticleVO> {

    private static Logger LOG = LoggerFactory.getLogger(ArticleSolrService.class);

    @Autowired
    private SolrContext solrContext;


    public PageVO<ArticleVO> searchArticle(String key, Pageable pageable) {


        PageVO<ArticleVO> search = null;
        try {
            search = super.search(key, solrContext.getDataCore(), pageable);
            LOG.info("全文搜索数据结果：【{}】",search);
        } catch (IOException |SolrServerException e) {
            LOG.error(e.getMessage(),e);
        }
        return search;
    }



    public void addArticle(ArticleVO articleVO){
        try {
            Boolean bool = super.add(solrContext.getDataCore(), articleVO);
            LOG.info("添加全文搜索数据结果：【{}】",bool);
        } catch (IOException |SolrServerException e) {
            LOG.error(e.getMessage(),e);
        }
    }


    public void deleteByTitle(String title){
        try{
            super.deleteByString(solrContext.getDataCore(),"art_title:"+title);
        }catch (IOException |SolrServerException e) {
        LOG.error(e.getMessage(),e);
    }
    }

    public void deleteById(String id) {
        try{
            super.deleteById(solrContext.getDataCore(),id);
        }catch (IOException |SolrServerException e) {
            LOG.error(e.getMessage(),e);
        }
    }

    public void updateArticle(ArticleVO articleVO) {

        try {
            Boolean bool = super.add(solrContext.getDataCore(), articleVO);
            LOG.info("添加全文搜索数据结果：【{}】",bool);
        } catch (IOException |SolrServerException e) {
            LOG.error(e.getMessage(),e);
        }
    }
}
